package com.pactera.madp.cp.controller.oms.marketing;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.pactera.madp.common.core.util.R;
import com.pactera.madp.common.log.annotation.SysLog;
import com.pactera.madp.cp.api.dto.marketing.MarketingPosterCreateDTO;
import com.pactera.madp.cp.api.dto.marketing.MarketingPosterUpdateDTO;
import com.pactera.madp.cp.api.entity.marketing.MarketingPosterEntity;
import com.pactera.madp.cp.api.vo.marketing.MarketingPosterVO;
import com.pactera.madp.cp.service.MarketingPosterService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * (McMarketingPoster)表控制层
 *
 * @author yushen.qi
 * @since 2021-05-24 18:55:14
 */
@RestController
@RequestMapping("/marketing-poster")
@Api(value = "marketingPoster", tags = "营销素材-海报管理")
public class MarketingPosterController {

    /**
     * 服务对象
     */
    @Resource
    private MarketingPosterService marketingPosterService;

    /**
     * 查询
     *
     * @param classification
     * @return
     */
    @GetMapping("/index")
    @ApiOperation(value = "查询")
    public R<List<MarketingPosterVO>> qryPage(Integer classification) {
        return R.ok(marketingPosterService.selectAllOrName(classification));
    }

    /**
     * 新增
     *
     * @param
     * @return
     */
    @SysLog("新增营销素材-海报")
    @PostMapping
    @ApiOperation(value = "新增")
    public R<Map<String,Integer>> saveArticle(@RequestBody MarketingPosterCreateDTO dto) {
        return R.ok(marketingPosterService.insert(dto));
    }

    /**
     * 编辑
     *
     * @param
     * @return
     */
    @SysLog("编辑营销素材-海报")
    @PutMapping
    @ApiOperation(value = "编辑")
    public R<Boolean> putArticle(@RequestBody MarketingPosterUpdateDTO marketingPosterUpdateDto) {
        return marketingPosterService.update(marketingPosterUpdateDto)?R.ok():R.failed("修改失败");
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @SysLog("删除营销素材-海报")
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除")
    public R<Boolean> delete(@PathVariable Integer id) {
        return  marketingPosterService.remove(id);
    }

    /**
     * 发布海报
     *
     * @param id
     * @return
     */
    @SysLog("发布营销素材-海报")
    @GetMapping("/release/{id}")
    @ApiOperation(value = "发布海报")
    public R<Boolean> releaseExcel(@PathVariable Integer id){
        MarketingPosterEntity posterEntity = new MarketingPosterEntity();
        posterEntity.setUpdateTime(LocalDateTime.now());
        posterEntity.setMarketingState(2);
        return marketingPosterService.update(posterEntity, Wrappers.<MarketingPosterEntity>lambdaQuery().eq(MarketingPosterEntity::getId,id))?R.ok(null,"发布成功"):R.failed("发布失败");
    }

}
